<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      v-loading="$store.getters.loading"
      :header-cell-style="tableHeaderCellStyle"
      :cell-style="cellStyle"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="#a19f9fb0;"
      :column="tableData.column"
      :data="tableData.data"
      height="680"
      border
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <!-- 反馈对话框 -->
    <el-dialog
      title="人工批示"
      :visible.sync="show"
      width="500px"
      append-to-body
    >
      <el-form ref="editForm" :model="model" :rules="rules" label-width="100px">
        <el-form-item label="分析意见" prop="opinion">
          <el-input
            type="textarea"
            v-model="model.opinion"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-hasPermi="['energy:warning-manage:save']"
          type="primary"
          @click="submit"
          >确 定</el-button
        >
        <el-button @click="close('form')">取 消</el-button>
      </div>
    </el-dialog>
  </lb-layout>
</template>

<script>
import { list, alarmConfirm, opinion } from "@/api/forewarning/alarm";
export default {
  name: "alarmCommand",
  dicts: [
    "warning_instruct_type",
    "warnning_level",
    "energy_warning_send_type",
    "energy_warning_opinions",
    "energy_warning_send_status",
  ],
  data() {
    return {
      show: false,
      model: {
        id: null,
        opinion: "",
      },
      rules: {
        opinion: [
          {
            required: true,
            message: "请输入分析意见",
          },
        ],
      },
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            //这里是渲染查询表单的表头和类型的数据
            date: {
              label:"日期范围:",
              typeName: "el-date-picker",
              type: "daterange",
              size: "small",
              "value-format": "yyyy-MM-dd",
              "start-placeholder": "开始日期",
              "end-placeholder": "结束日期",
            },
            instructType: {
              label:"报警类型:",
              typeName: "el-select",
              render: (h) => {
                let that = this;
                let arr = [];
                that.dict?.type?.warning_instruct_type.some((item) => {
                  arr.push(item);
                });
                // 数据严谨处理,处理后端数据异常情况
                let valNum = that.$refs?.form?.getFieldsValue().instructType;
                let a = [
                  h(
                    "el-select",
                    {
                      attrs: {
                        placeholder: "请选择报警类型",
                        value: valNum,
                        clearable: true,
                      },
                      props: ["value"],
                      on: {
                        change: (value) => {
                          that.$refs?.form?.setFieldValue(
                            "instructType",
                            value
                          );
                          that.$forceUpdate();
                        },
                      },
                    },
                    [
                      arr.map((item) => {
                        let { label, value } = item;
                        return h("el-option", {
                          props: {
                            label,
                            value: parseInt(value),
                            key: value,
                          },
                        });
                      }),
                    ]
                  ),
                ];
                return h("div", a);
              },
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "导出",
              type: "success",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-upload2",
              click: this.handleDerive,
            },
          ],
        },
      },
      tableData: {
        column: [
          {
            prop: "warningObject",
            label: "预警对象",
            showOverflowTooltip: true,
            minWidth: 100,
          },
          {
            prop: "instructType",
            label: "报警类型",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.warning_instruct_type}
                    value={scope.row.instructType}
                  />
                </div>
              );
            },
          },
          {
            prop: "level",
            label: "报警等级",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.warnning_level}
                    value={scope?.row?.level}
                  />
                </div>
              );
            },
          },
          {
            prop: "description",
            label: "报警描述",
            showOverflowTooltip: true,
            minWidth: 100,
          },
          {
            prop: "sendType",
            label: "发送模式",
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_warning_send_type}
                    value={scope.row.sendType}
                  />
                </div>
              );
            },
          },
          {
            prop: "sendTime",
            label: "发送时间",
            width: 180,
          },
          {
            prop: "plans",
            label: "分析及建议方案",
            minWidth: 150,
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_warning_opinions}
                    value={scope?.row?.plans?.split(",")}
                  />
                </div>
              );
            },
          },
          {
            prop: "sendStatus",
            label: "状态",
            minWidth: 120,
            showOverflowTooltip: true,
            render: (h, scope) => {
              return (
                <div>
                  <dict-tag
                    options={this.dict.type.energy_warning_send_status}
                    value={scope.row.sendStatus}
                  />
                </div>
              );
            },
          },
          {
            prop: "opinion",
            label: "分析意见",
            showOverflowTooltip: true,
            minWidth: 120,
          },
          {
            label: "操作",
            width: "200",
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              if (scope.row.sendStatus === 0) {
                return (
                  <div>
                    {
                      <el-button
                        type="text"
                        size="small"
                        icon="el-icon-check"
                        v-hasPermi={["energy:warning-manage:confirm"]}
                        onClick={() => {
                          this.handleConfirm(scope.row, 1);
                        }}
                      >
                        确认
                      </el-button>
                    }
                    {
                      <el-button
                        type="text"
                        size="small"
                        icon="el-icon-close"
                        v-hasPermi={["energy:warning-manage:confirm"]}
                        onClick={() => {
                          this.handleConfirm(scope.row, 2);
                        }}
                      >
                        误报
                      </el-button>
                    }
                    {
                      <el-button
                        v-hasPermi={["energy:neutralNeutral:confirm"]}
                        size="small"
                        type="text"
                        icon="el-icon-document-checked"
                        onClick={() => {
                          this.handleOpinion(scope.row);
                        }}
                      >
                        人工分析
                      </el-button>
                    }
                  </div>
                );
              } else {
                return (
                  <div>
                    {
                      <el-button
                        v-hasPermi={["energy:neutralNeutral:confirm"]}
                        size="small"
                        type="text"
                        icon="el-icon-document-checked"
                        onClick={() => {
                          this.handleOpinion(scope.row);
                        }}
                      >
                        人工分析
                      </el-button>
                    }
                  </div>
                );
              }
            },
          },
        ],
        data: [],
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
    };
  },
  mounted() {
    this.initData();
  },
  methods: {
    tableHeaderCellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 7) {
        return "background:rgba(223,240,216,0.4)";
      } else if (columnIndex === 8) {
        return "background:rgba(242,222,222,0.4)";
      } else if (columnIndex === 9) {
        return "background:rgba(217,237,247,0.4)";
      }
    },
    //设置指定行、列、具体单元格颜色
    cellStyle({ row, column, rowIndex, columnIndex }) {
      //指定坐标rowIndex ：行，columnIndex ：列
      if (columnIndex === 7) {
        return "background:rgba(223,240,216,0.4)";
      } else if (columnIndex === 8) {
        return "background:rgba(242,222,222,0.4)";
      } else if (columnIndex === 9) {
        return "background:rgba(217,237,247,0.4)";
      }
    },
    async initData(currentPage, pageSize, data) {
      let that = this;
      let query = {
        ...data,
      };
      if (currentPage) {
        query.pageNum = currentPage;
      } else {
        query.pageNum = that.currentPage;
      }
      if (pageSize) {
        query.pageSize = pageSize;
      } else {
        query.pageSize = that.pageSize;
      }
      this.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          this.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            that.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res.rows;
          that.total = res.total;
        })
        .catch((err) => {
          this.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!300111");
        });
    },
    handlerSearchDetail(val) {
      //val===1 删除操作成功后分页边界值处理
      let currentPage = 1;
      if (val === 1) {
        if (
          this.total === (this.currentPage - 1) * this.pageSize + 1 &&
          this.total !== 0 &&
          this.currentPage > 1
        ) {
          currentPage = this.currentPage - 1;
          this.currentPage = currentPage;
        } else {
          currentPage = this.currentPage;
        }
      } else {
        currentPage = this.currentPage;
      }
      const { date, ...other } = this.$refs?.form?.getFieldsValue();
      const [startTime, endTime] =
        this.$refs?.form?.getFieldsValue().date || [];
      const data = {
        ...other,
        endTime,
        startTime,
      };
      this.initData(currentPage, this.pageSize, data);
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    },
    //重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    // 导出
    handleDerive() {},
    /** 人工确认 */
    handleConfirm(row, type) {
      let text = type === 1 ? "确认要人工确认发送吗？" : "确认要误报吗？";
      this.$modal
        .confirm(text)
        .then(function () {
          alarmConfirm({ id: row.id, operationType: type })
            .then((res) => {
              if (res.code === 700) {
                this.$message.warning(res.msg);
                return false;
              }
              this.$modal.msgSuccess("操作成功");
              this.handlerSearchDetail();
            })
            .catch(function () {
              // this.$message.error("系统错误,请联系管理员!300502");
            });
        })
        .catch(() => {});
    },
    close() {
      this.resetForm("editForm");
      this.show = false;
    },
    /** 人工分析 */
    handleOpinion(row) {
      this.show = true;
      this.model.id = row.id;
      this.model.opinion = row.opinion;
      this.$nextTick(() => {
        this.$refs["editForm"].clearValidate();
      });
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          opinion(this.model)
            .then((res) => {
              if (res.code === 700) {
                this.$message.warning(res.msg);
                return false;
              }
              this.$message.success("操作成功");
              this.close();
              this.handlerSearchDetail();
            })
            .catch((err) => {
              // this.$message.error("系统错误,请联系管理员!300503");
            });
        }
      });
    },
  },
};
</script>
